package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.TeamRelevanceType;
import cn.cdeden.knowledge.domain.dto.TeamRelevanceTypeDto;
import cn.cdeden.knowledge.domain.dto.TeamRelevanceTypeQueryParam;
import cn.cdeden.knowledge.mapper.TeamRelevanceTypeMapper;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.knowledge.service.TeamRelevanceTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
* @author Chen
* @date 2024-04-21
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = TeamRelevanceTypeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamRelevanceTypeServiceImpl extends ServiceImpl<TeamRelevanceTypeMapper, TeamRelevanceType> implements TeamRelevanceTypeService {

    private final TeamRelevanceTypeMapper teamRelevanceTypeMapper;

    @Override
    public TableDataInfo<TeamRelevanceTypeDto> queryAll(TeamRelevanceTypeQueryParam query, Pageable pageable) {
        IPage<TeamRelevanceType> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<TeamRelevanceType> page = teamRelevanceTypeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), TeamRelevanceTypeDto.class);
    }

    @Override
    public List<TeamRelevanceTypeDto> queryAll(TeamRelevanceTypeQueryParam query){
        return MapstructUtils.convert(teamRelevanceTypeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), TeamRelevanceTypeDto.class);
    }

    @Override
    public TeamRelevanceType getById(Integer id) {
        return teamRelevanceTypeMapper.selectById(id);
    }

    @Override
    public List<String> getByTypeId(String typeId) {
        return teamRelevanceTypeMapper.getByTypeId(typeId, LoginHelper.getUserId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(TeamRelevanceTypeDto resources) {
        TeamRelevanceType entity = MapstructUtils.convert(resources, TeamRelevanceType.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(TeamRelevanceTypeDto resources){
        TeamRelevanceType entity = MapstructUtils.convert(resources, TeamRelevanceType.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Integer> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    @Override
    public boolean deleteRelevanceType(TeamRelevanceTypeDto resources) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("team_id",resources.getTeamId());
        columnMap.put("type_id",resources.getTypeId());
        columnMap.put("user_id",resources.getUserId());
        teamRelevanceTypeMapper.deleteByMap(columnMap);
        return true;
    }

    @Override
    public boolean deleteTypeId(Integer typeId) {
        return teamRelevanceTypeMapper.deleteTypeId(typeId);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<TeamRelevanceTypeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (TeamRelevanceTypeDto teamRelevanceType : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("团队id", teamRelevanceType.getTeamId());
              map.put("用户id", teamRelevanceType.getUserId());
              map.put("分类id", teamRelevanceType.getTypeId());
              map.put("创建者", teamRelevanceType.getCreateId());
              map.put("创建时间", teamRelevanceType.getCreateTime());
              map.put("更新者", teamRelevanceType.getUpdateId());
              map.put("更新时间", teamRelevanceType.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
